<?php

function __autoload($class_name) {
	include_once (CS_CORE_DIR . $class_name . '.class.php');
}

function exception_handler($exception) {
	echo "Error<br />\n";
	if (defined('CS_ENABLE_DEBUG') && CS_ENABLE_DEBUG) {
		echo "Message: ", $exception -> getMessage(), "<br />\n";
		echo "Code: ", $exception -> getCode(), "<br />\n";
		echo "File: ", $exception -> getFile(), "<br />\n";
		echo "Line: ", $exception -> getLine(), "<br />\n";
	}
}

function initialize() {
	set_exception_handler('exception_handler');
}

function translate($text, $lang = null) {
	$l = ($lang ? $lang : '');
	
	if (isset($_GLOBALS['trans_tbl'][$l][$text])) {
		$text = $_GLOBALS['trans_tbl'][$l][$text];
	}
	
	return $text;
}

function debug_var($var) {
	echo "<pre>";
	var_dump($var);
	echo "</pre>";
}

function get_request() {
	$cs_params = $_REQUEST;
	
	return $cs_params;
}

function prepare_params($params, $pass_params = null, $add_params = null) {
	$result_params = array(
	);
	
	if (is_array($pass_params)) {
		foreach ($params as $p_name => $p_val) {
			if (in_array($p_name, $pass_params)) {
				$result_params[$p_name] = $p_val;
			}
		}
	}
	
	if (is_array($add_params)) {
		$result_params = array_merge($result_params, $add_params);
	}
	
	$result_params = unroll_tables($result_params);
	
	return $result_params;
}

function url_params($params, $pass_params = null, $add_params = null) {
	$parr = prepare_params($params, $pass_params, $add_params);
	$query_string = "";
	
	foreach ($parr as $p_name => $p_val) {
		if ($p_val !== null) {
			if ($query_string)
				$query_string .= "&";
			$query_string .= urlencode($p_name) . "=" . urlencode($p_val);
		}
	}
	
	if ($query_string)
		$query_string = "?$query_string";
	
	return $query_string;
}

function hidden_params($params, $pass_params = null, $add_params = null) {
	$parr = prepare_params($params, $pass_params, $add_params);
	$hidden_string = "";
	
	foreach ($parr as $p_name => $p_val) {
		if ($p_val) {
			$hidden_string .= "<input type=\"hidden\" name=\"$p_name\" value=\"$p_val\" />\n";
		}
	}
	
	return $hidden_string;
}

function unroll_tables($arr, $prefix = null) {
	$retarr = array(
	);
	
	while (list ($arg_name, $arg_value) = each($arr)) {
		if (is_array($arg_value)) {
			$retarr = array_merge($retarr, unroll_tables($arg_value, ($prefix ? "$prefix" . "[$arg_name]" : $arg_name)));
		} else if ($prefix) {
			$retarr["$prefix" . "[$arg_name]"] = $arg_value;
		} else {
			$retarr[$arg_name] = $arg_value;
		}
	}
	return $retarr;
}

function cmp_obj_sort(&$a, &$b) {
	if ($a -> sort == $b -> sort) {
		return 0;
	}
	return ($a -> sort < $b -> sort) ? - 1 : 1;
}

class csException extends Exception {

	public function __construct($message = null, $code = 0) {
		if (! $message) {
			$message = 'Error';
		}
		
		parent::__construct($message, $code);
	}

}

class csMysqlException extends csException {
	protected $mysql_error;
	protected $mysql_query;

	public function __construct($message = null, $code = 0, $query = null) {
		$this -> mysql_error = mysql_error();
		$this -> mysql_query = $query;
		
		parent::__construct($message, $code);
	}

	public function get_mysql_error() {
		return $this -> mysql_error;
	}

	public function get_mysql_query() {
		return $this -> mysql_query;
	}
}

class csErrorException extends csException {
}

class csFatalException extends csException {
}

class csWarning {
	public $message = 'Unknown warning';
	public $code = 0;

	//TODO - moze jescze jedna wlasnosc - comment
	

	public function __construct($message = null, $code = 0) {
		if ($message)
			$this -> message = $message;
		if ($code)
			$this -> code = $code;
	}

	public function get_message() {
		return $this -> message;
	}

	public function get_code() {
		return $this -> code;
	}

	public function to_string() {
		return $this -> get_message();
	}
}

class csWarningQueue {
	protected $warning_tbl;

	public function __construct() {
		$this -> warning_tbl = array(
		);
	}

	public function add_warning($warningObj) {
		if (! ($warningObj instanceof csWarning)) {
			throw new csErrorException(translate('Incorrect csWarning object'), 888032);
		}
		
		$this -> warning_tbl[] = $warningObj;
	}

	public function is_warning() {
		if ($this -> warning_tbl) {
			return true;
		} else {
			return false;
		}
	}

	public function get_warning_tbl() {
		$ret_tbl = array(
		);
		
		foreach ($this -> warning_tbl as $idw => $warning_obj) {
			$ret_tbl[$idw] = array(
				'message' => $warning_obj -> get_message(), 
				'code' => $warning_obj -> get_code(), 
				'string' => $warning_obj -> to_string()
			);
		}
		
		return $ret_tbl;
	}
}

?>
